using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using Lib.Helper;
using MSIACCSL2.Command;
using MSIACCSL2.CustomerServices;
using WcfService1.DTO;

namespace MSIACCSL2.ViewModels
{
    public class CustomerSearchViewModel : ViewModelBase
    {
        #region Delegates

        public delegate void Completed(Object param);

        #endregion

        public static Mode mode;
        private static CustomerDTO _Customer;
        public static Completed OnSearchCompleted;
        private readonly CustomerServicesClient client;
        private int CurrentPage;
        private PagedCollectionView _Customers;
        private bool _IsBusy;
        private List<string> _errors;

        public CustomerSearchViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                client = new CustomerServicesClient();
                SearchCommand = new DelegateCommand(SearchCustomer, CanSearch);
            }
        }

        public PagedCollectionView Customers
        {
            get { return _Customers; }

            private set
            {
                if (Customers == value) return;
                _Customers = value;
                int i = (CurrentPage*30);
                foreach (object Customer in Customers)
                {
                    i += 1;
                    ((CustomerDTO) Customer).No = i;
                }
                FirePropertyChanged("Customers");
            }
        }


        public bool IsBusy
        {
            get { return _IsBusy; }
            internal set
            {
                _IsBusy = value;
                FirePropertyChanged("IsBusy");
            }
        }


        public long RecordCount { get; set; }

        //Buat binding ke child form waktu mau edit
        public static CustomerDTO Customer
        {
            get { return _Customer; }
            private set
            {
                if (Customer == value) return;
                _Customer = value;
                FirePropertyChanged2("Customer");
            }
        }


        public ICommand OnPageChanged { get; set; }
        public ICommand Save { get; set; }
        public ICommand New { get; set; }
        public ICommand Edit { get; set; }
        public ICommand Unload { get; set; }
        public ICommand SearchCommand { get; set; }
        public ICommand SearchResultCommand { get; set; }

        public List<string> Errors
        {
            get { return _errors; }
            private set
            {
                if (Errors == value) return;
                _errors = value;
                FirePropertyChanged("Errors");
            }
        }

        public void SearchCustomer(Object param)
        {
            if (param == null) return;
            client.GetCustomerListCompleted += client_GetCustomerListCompleted;
            client.GetCustomerListAsync(param.ToString());
        }

        private void client_GetCustomerListCompleted(object sender, GetCustomerListCompletedEventArgs e)
        {
            client.GetCustomerListCompleted -= client_GetCustomerListCompleted;
            Customers = new PagedCollectionView(e.Result);
            if (Customers.Count < 1)
                MessageBox.Show("Customer tidak ditemukan!");
        }

        public bool CanSearch(Object param)
        {
            return true;
        }
    }
}